<?php
class ProfileController extends Model3_Controller
{
    public function init()
    {
        $credentials = Model3_Auth::getCredentials();
        if($credentials != null)
        {
            $this->view->idUsuario = $credentials['idUsuario'];
        }
        else
        {
            $this->redirect('Index/index'); // si no se tiene que mostrar regresarlo a  portada
        }
        
    }

    
    protected function indexAction()
    {
        $userAdapter = MN_UsuariosTable::getInstance();
        $user = $userAdapter->getUserById($this->view->idUsuario);
        if(!($user instanceof MN_Usuarios))
        {
            $user = false;
        }
        
        if($this->view->idUsuario == $user->idUsuario)
        {  // cargando JEIP
            $this->view->getJsManager()->addJs('jeip.js');
            $this->view->getJsManager()->addJs('application/Profile/initJEIP.js');
            
        }
        
        $this->view->profile = $user;
    }
    
    public function createOwnerAction()
    {
        $clase = '';
        $message = '';
        if($this->getRequest()->isPost())
        {
            $post = $this->getRequest()->getPost();
            
            $user = new MN_Usuarios;
            $date = new DateTime();
            
            $user->nombre = $post['nombre'];
            $user->apellidoPaterno = $post['apaterno'];
            $user->apellidoMaterno = $post['amaterno'];
            $user->username = $post['email'];
            $user->password = md5($post['pass']);
            $user->sexo = $post['sex'];
            $user->tipoUsuario = MN_Usuarios::TYPE_OWNER;
            $user->status = MN_Usuarios::STATUS_NORMAL;
            $user->fechaIngreso = $date->format('Y-m-d H:i:s');
            
            if($user->trySave())
            {
                $this->redirect('SignUp/loginForGet/id/'.$user->idUsuario.'/pass/'.$post['pass'].'/');
            }
            
        }
    }
    
    public function saveSimpleDataAction()
    {
        $userAdapter = MN_UsuariosTable::getInstance();
        $res = new stdClass();
        $res->is_error = true;
        $res->error_text = 'No se ha podido actualizar su perfil, intentelo mas tarde';
        $res->html  = '';
        $textReg = '';
        
        if( $this->getRequest()->isPost())
        {
            $post = $this->getRequest()->getPost();
            $identificador = $post['id'];
            $user = $userAdapter->getUserById($this->view->idUsuario);
            if($user instanceof MN_Usuarios)
            {
               switch ($identificador) {
                    case 'name':
                        $textReg = $user->nombre = $post['new_value'];
                        break;
                    case 'apaterno':
                        $textReg = $user->apellidoPaterno = $post['new_value'];
                        break;
                    case 'amaterno':
                        $textReg = $user->apellidoMaterno = $post['new_value'];
                        break;
                    case 'direccion':
                        $textReg = $user->direccion = $post['new_value'];
                        break;
                    case 'facebook':
                        $textReg = $user->cuenta_facebook = $post['new_value'];
                        break;
                    case 'twitter':
                        $textReg = $user->cuenta_twitter = $post['new_value']; 
                        break;
                } 
            }
            
            if($user->trySave())
            {
                $res->html = $textReg;
                $res->is_error = false;
                $res->error_text = 'Informacion guardada.';
            }
        }
        
        $this->view->res = $res;
        $this->view->setUseTemplate(false);
    }
     

}
